package smartbooks.ui.manager;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import smartbooks.exceptions.MyException;
import smartbooks.ui.helper.InvoiceHelper;
import smartbooks.ui.util.CommonConstants;
import smartbooksdb.exception.CustomerException;
import smartbooksdb.exception.InvoiceException;
import smartbooksdb.exception.InvoiceLinesException;
import smartbooksdb.exception.MandatoryFieldException;
import smartbooksdb.hibernatebean.Invoice;
import smartbooksdb.hibernatebean.InvoiceLines;
import smartbooksdb.service.InvoiceLinesService;
import smartbooksdb.service.InvoiceService;

/**
 *
 * @author Ankit
 */
public class InvoiceManager implements CommonConstants {
    
    /**
     * This method is used to create invoice
     * @param invoice
     * @param invoiceLinesList
     * @return 'true' if the invoice is created or else 'false'
     * @throws MyException
     * @throws MandatoryFieldException
     */
    public static boolean createInvoice(Invoice invoice, ArrayList<InvoiceLines> invoiceLinesList) throws MyException, MandatoryFieldException {
        try {
            System.out.println(invoice.toString());
            System.out.println(invoiceLinesList.toString());
            InvoiceService invoiceService = new InvoiceService();
            InvoiceLinesService invoiceLinesService = new InvoiceLinesService();
            
            boolean invoicePanelValidated = InvoiceHelper.validateInvoice(invoice);
            
            if(!invoiceService.isInvoiceNoAvailable(invoice.getInvoiceNo(), true)) {
                throw new MyException(INVOICE_NO, "Invoice No unavailable.");
            }
            
            if(invoicePanelValidated) {
                long invoiceId = invoiceService.createInvoice(invoice);
                
                for(InvoiceLines invoiceLines : invoiceLinesList) {
                    invoiceLines.setInvoiceId(invoiceId);
                }
                if(null!=invoiceLinesList && invoiceLinesList.size()>0) {
                    ArrayList<Long> invoiceLinesIdList = invoiceLinesService.createInvoiceLines(invoiceLinesList);
                }
                
                return true;
            }
        } catch (InvoiceException | InvoiceLinesException ex) {
            Logger.getLogger(InvoiceManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    
    /**
     * This method is used to get the balance of a particular customer using customerId
     * @param customerId
     * @return nextInvoiceNo
     */
    public static String getBalanceByCustomerId(long customerId) {
        DecimalFormat df = new DecimalFormat("0.00");
        df.setMaximumFractionDigits(2);
        
        InvoiceService invoiceService = new InvoiceService();
        try {
            //long totalInvoiceNetAmount = 0;
            //long totalInvoicePaymentAmount = 0;
            //long balance = 0;
            //ArrayList<Invoice> invoices = (ArrayList)invoiceService.getAllActiveInvoicesByCustomerId(customerId);
            return df.format(invoiceService.getBalanceByCustomerId(customerId));
        } catch(CustomerException ex) {
            return "";
        }
    }
    
    /**
     * This method is used to get the next Invoice No
     * @return nextInvoiceNo
     */
    public static String getNextInvoiceNo() {
        InvoiceService invoiceService = new InvoiceService();
        
        ArrayList<String> invoiceNos = (ArrayList)invoiceService.getAllInvoiceNos();
        
        String nextInvoiceNo = DEFAULT_INVOICE_NO;
        
        for(int i=0; i<invoiceNos.size(); i++) {
            String invoiceNo = invoiceNos.get(i);
            
            if(invoiceNo.equals(nextInvoiceNo)) {
                nextInvoiceNo = String.valueOf(Integer.parseInt(nextInvoiceNo)+1);
                i=0;
            }
        }
        
        return nextInvoiceNo;
    }
}
